package com.liao.controller;

import com.alibaba.fastjson.JSON;
import com.liao.po.ObjectItem;
import com.liao.utils.MinioUtilS;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@Slf4j
public class MinioController {

    @Autowired
    private MinioUtilS minioUtilS;
    @Value("${minio.endpoint}")
    private String address;
    @Value("${minio.bucketName}")
    private String bucketName;


    @PostMapping("/upload")
    public String upload(MultipartFile file) {

        List<String> upload = minioUtilS.upload(new MultipartFile[]{file});

        ObjectItem objectItem = new ObjectItem();


//        封装返回前端数据
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", upload.get(0));
        map.put("url", address+"/"+bucketName+"/"+upload.get(0));

        String mapJson = JSON.toJSONString(map);


        return mapJson;
    }

}

